<?php
	
	if (!class_exists('ctl_order'))
	{
		require_once(CORE_DIR.'/shop/controller/ctl.order.php');
	}
	class collocate_goods_order_ctl extends ctl_order
	{
		function create()
		{
			$this->begin($this->system->mkUrl('cart', 'checkout'));
			$this->_verifyMember(false);
			
			$objCollocOrder = $this->system->loadModel('plugins/collocate_goods/collocOrder');
			$objOrder = &$this->system->loadModel('trading/order');
			$oCart = &$this->system->loadModel('trading/cart');
			$oCart->checkMember($this->member);
			if($_POST['isfastbuy']){
				$cart = $oCart->getCart('all',$_COOKIE['Cart_Fastbuy']);
			}else{
				$cart = $oCart->getCart('all');
			}
			
			
		   if($_POST['delivery']['ship_addr_area']!=''){
				
				$_POST['delivery']['ship_addr'] = str_replace(' ','',$_POST['delivery']['ship_addr_area'].$_POST['delivery']['ship_addr']);
			}
			
			
			
			$orderid = $objCollocOrder->create($cart, $this->member,$_POST['delivery'],$_POST['payment'],$_POST['minfo'],$_POST);
			if($orderid){
				if($_POST['fromCart'] && !$_POST['isfastbuy']){
					$oCart->removeCart();
				}
	/*             $this->redirect('index','order',array($orderid)); */
			}else{
				trigger_error(__('对不起，订单创建过程中发生问题，请重新提交或稍后提交'),E_USER_ERROR);            
			}
			$this->system->setcookie('ST_ShopEx-Order-Buy', md5($this->system->getConf('certificate.token').$orderid));
			$account=$this->system->loadModel('member/account');
			$account->fireEvent('createorder',$this->member,$this->member['member_id']);
			$this->end_only(true, __('订单建立成功'), $this->system->mkUrl('order', 'index', array($orderid)));
			
			$GLOBALS['pageinfo']['order_id'] = $orderid;
			$this->redirect('order','index',array($orderid));
		}

		function detail($order_id, $selecttype=false){
			$this->customer_template_type='order_detail';
			if($_COOKIE['ST_ShopEx-Order-Buy'] != md5($this->system->getConf('certificate.token').$order_id)){
				$this->splash('failed','index.php',__('订单无效！'));
			}
			$this->title = '订单详细';
			$objOrder = &$this->system->loadModel('trading/order');
			$aOrder = $objOrder->load($order_id);
			$aOrder['member_id'] = is_null($aOrder['member_id'])?false:$aOrder['member_id'];
			$this->_verifyMember($aOrder['member_id']);
			$aOrder['cur_money'] = ($aOrder['amount']['total'] - $aOrder['amount']['payed']) * $aOrder['cur_rate'];
			if($aOrder['member_id']){
				$member = &$this->system->loadModel('member/member');
				$aMember = $member->getFieldById($aOrder['member_id'], array('email'));
				$aOrder['receiver']['email'] = $aMember['email'];
			}
			$shiparea = explode( ':', $aOrder['receiver']['area'] );
			$aOrder['shipping']['area'] = $shiparea[1];
			$this->pagedata['order'] = $aOrder;
			if(!$this->pagedata['order']){
				$this->system->error(404);
				exit;
			}
			$gItems = $objOrder->getItemList($order_id);
			foreach($gItems as $key => $item){
				$gItems[$key]['addon'] = unserialize($item['addon']);
				if($item['minfo'] && unserialize($item['minfo'])){
					$gItems[$key]['minfo'] = unserialize($item['minfo']);
				}else{
					$gItems[$key]['minfo'] = array();
				}
			}
			$this->pagedata['order']['items'] = $gItems;
			$this->pagedata['order']['giftItems'] = $objOrder->getGiftItemList($order_id);
			
			if($selecttype){
				$selecttype = 1;
	//            $shipping = &$this->system->loadModel('trading/delivery');
	//            $this->pagedata['delivery'] = $shipping->checkDlTypePay($this->pagedata['order']['shipping']['id'], $this->pagedata['order']['shipping']['area']);
				$payment = &$this->system->loadModel('trading/payment');
				$this->pagedata['payments'] = $payment->getByCur($this->pagedata['order']['currency']);
			}else{
				$selecttype = 0;
			}
			$this->pagedata['order']['selecttype'] = $selecttype;
			
			$objCur = &$this->system->loadModel('system/cur');
			$aCur = $objCur->getDefault();
			$this->pagedata['order']['cur_def'] = $aCur['cur_code'];
			/* alone add start */
			//是否有搭配商品
			$objCollocOrder = &$this->system->loadModel('plugins/collocate_goods/collocOrder');
			$aCoItems = $objCollocOrder->getCollocItemList($order_id);
			if (!empty($aCoItems) && is_array($aCoItems))
			{
				foreach ($aCoItems as $k => $v)
				{
					//主商品p_id为0 主商品的co_id和搭配商品的p_id是相同的
					if ('0' == $v['p_id'])
					{
						$coGoods[$v['co_id']]['goods'] = $v;
						$coGoods[$v['co_id']]['goods']['loopKey'] = 'co'.$k;
					}
					else
					{
						$coGoods[$v['p_id']]['items']['co'.$k] = $v;
					}
				}

				$this->pagedata['showColloc'] = true;
				$this->pagedata['collocGoods'] = &$coGoods;
			}
			$this->pagedata['orderinfo_path'] = dirname(__FILE__).'/view/shop/common/orderinfo.html';
			$this->pagedata['_MAIN_'] = dirname(__FILE__).'/view/shop/order/detail.html';
			/* alone add end */
			$this->output();
		}
	}
?>